package main

import (
	_ "encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"
	//"github.com/satori/go.uuid"
)

/*
	模拟修改时间

************************
# 在 Linux 上安装
sudo apt-get install libfaketime
**********************
*/
const myIp = "43.159.132.218"

func setSystemTime(t time.Time) error {
	// 使用 time.Now() 函数获取当前时间
	currentTime := time.Now()

	// 计算时间差
	timeDiff := t.Sub(currentTime)

	// 使用 time.Add 函数设置系统时间
	time.Now().Add(timeDiff).Unix()

	return nil
}
func initTime() {
	// 读取 FAKETIME_TIMESTAMP_FILE 环境变量
	timeFilePath := os.Getenv("FAKETIME_TIMESTAMP_FILE")
	if timeFilePath == "" {
		fmt.Println("FAKETIME_TIMESTAMP_FILE environment variable not set")
		return
	}

	// 读取时间文件的内容
	timeBytes, err := ioutil.ReadFile(timeFilePath)
	if err != nil {
		fmt.Printf("Error reading file: %v\n", err)
		return
	}

	// 将时间文件的内容转换为 time.Time 类型
	fakeTime, err := time.Parse("2006-01-02 15:04:05", string(timeBytes))
	if err != nil {
		fmt.Printf("Error parsing time: %v\n", err)
		return
	}

	// 设置 time.Local 变量为自定义的时区
	time.Local = fakeTime.Location()

	// 现在所有的时间获取操作都会使用自定义的时区
	currentTime := time.Now()
	fmt.Println(currentTime) // 输出修改后的时间

	fmt.Printf("System time set to: %s\n", fakeTime.Format("2006-01-02 15:04:05"))

}
func main() {
	initTime()
	// 设置 LD_PRELOAD 环境变量加载 libfaketime 库
	//os.Setenv("LD_PRELOAD", "/usr/local/lib/faketime/libfaketime.so.1")

	// 设置 FAKETIME_TIMESTAMP_FILE 环境变量指定时间戳文件
	//os.Setenv("FAKETIME_TIMESTAMP_FILE", "/path/to/timestamp.txt")

	// 设置 FAKETIME_TIMESTAMP_FILE 环境变量指定时间戳文件
	//os.Setenv("FAKETIME_TIMESTAMP_FILE", "/home/go_pro/Exec/timestamp.txt")

	// 手动设置 LD_PRELOAD 环境变量
	os.Setenv("LD_PRELOAD", "/usr/local/lib/faketime/libfaketime.so.1")
	// 尝试加载 libfaketime
	fakeTime := os.Getenv("FAKE_TIME")
	if fakeTime != "" {
		fmt.Println("Faketime enabled, setting time to", fakeTime)
	} else {
		fmt.Println("Faketime not enabled")
	}
	/*
		http.Handle("/s/", http.StripPrefix("/s/", http.FileServer(http.Dir("sourse"))))
		//http.HandleFunc("/", http.FileServer(http.Dir("js")))
		//http.handle("/", http.FileServer(http.Dir("js")))
		//http.ListenAndServe("8081", nil)
		http.ListenAndServe(":1789", nil)
	*/

	mux := http.NewServeMux()
	mux.Handle("/", http.FileServer(http.Dir("www")))
	mux.HandleFunc("/index", fileHandler)
	mux.HandleFunc("/t", ServeHTTP)
	mux.HandleFunc("/js1.html", ServeHTTP2)
	// 添加获取当前时间的处理程序
	mux.HandleFunc("/time", getTime)

	server := &http.Server{
		Addr:    ":11081",
		Handler: mux,
	}

	listenAddr := server.Addr[1:]

	fmt.Println("==============================================")
	fmt.Println("Available services:")
	fmt.Printf("- http://%s:%s\n", myIp, listenAddr)
	fmt.Printf("- http://%s:%s/index\n", myIp, listenAddr)
	fmt.Printf("- http://%s:%s/t\n", myIp, listenAddr)
	fmt.Printf("- http://%s:%s/js1.html\n", myIp, listenAddr)
	fmt.Printf("- http://%s:%s/time\n", myIp, listenAddr)
	fmt.Println("==============================================")

	if err := server.ListenAndServe(); err != nil {
		log.Fatal(err)
	}
}

func fileHandler(w http.ResponseWriter, r *http.Request) {
	http.Redirect(w, r, "./js.html", http.StatusFound)
}

func ServeHTTP(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("The time is:"))
}

func ServeHTTP2(w http.ResponseWriter, r *http.Request) {
	content, err := os.ReadFile("www/js.html")
	if err != nil {
		panic(err)
	}
	fmt.Println(string(content))
	w.Write(content)
}

// 获取当前时间的处理程序
func getTime(w http.ResponseWriter, r *http.Request) {
	/* 测试无效
	println("系统时间", time.Now().Format(time.RFC3339))
	println("启动补丁>>>")
	faker := gofaketime.NewFakeTime()
	defer faker.Close()
	println("修改后的时间", time.Now().Format(time.RFC3339))
	println("运行完成")
	*/

	/*
		// 设置模拟的时间
		fakeTime := time.Date(2024, 5, 31, 12, 34, 56, 0, time.Local)
		// 获取当前时间
		currentTime1 := fakeTime
		fmt.Println(currentTime1) // 输出模拟的时间
		fmt.Fprintf(w, "服务器当前时间是: %s", currentTime1)
	*/

	// 获取当前工作目录
	workingDir, err := os.Getwd()
	if err != nil {
		fmt.Println("Error getting working directory:", err)
		return
	}
	// 打印当前工作目录
	fmt.Println("Current working directory:", workingDir)
	// 模拟时间
	fmt.Println("当前时间:", time.Now())

	currentTime := time.Now().Format("2006-01-02 15:04:05.000")
	fmt.Fprintf(w, "服务器当前时间是: %s", currentTime)

}
